<?php

/**
 * This is the model class for table "Contest".
 *
 * The followings are the available columns in table 'Contest':
 * @property string $id
 * @property string $name
 * @property string $description
 * @property integer $prizeId
 * @property string $submitStart
 * @property string $submitEnd
 * @property string $voteStart
 * @property string $voteEnd
 * @property string $created
 * @property string $updated
 * @property integer $deleted
 *
 * The following are the available relations in the table 'Contest':
 * @property array $entries
 * @property Prize $prize
 */
class Contest extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className the class name
	 * @return Contest the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Contest';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('name, description, prizeId, submitStart, submitEnd, voteStart, voteEnd', 'required'),
			array('prizeId, deleted', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>255),
			array('created, updated, deleted', 'safe'),
			array('id, name, description, prizeId, submitStart, submitEnd, voteStart, voteEnd, created, updated', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'entries' => array(self::HAS_MANY, 'Entry', 'contestId'),
			'prize' => array(self::BELONGS_TO, 'Prize', 'prizeId'),
		);
	}

	/**
	 * @return array attached behaviors.
	 */
	public function behaviors()
	{
		return array(
			'ext.auditbehavior.AuditBehavior',
			'ext.deletedbehavior.DeletedBehavior'
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('label', 'Id'),
			'name' => Yii::t('label', 'Name'),
			'description' => Yii::t('label', 'Description'),
			'prizeId' => Yii::t('label', 'Prize'),
			'submitStart' => Yii::t('label', 'Submit Start'),
			'submitEnd' => Yii::t('label', 'Submit End'),
			'voteStart' => Yii::t('label', 'Vote Start'),
			'voteEnd' => Yii::t('label', 'Vote End'),
			'created' => Yii::t('label', 'Created'),
			'updated' => Yii::t('label', 'Updated'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->with[] = 'prize';

		$criteria->compare('t.id',$this->id,true);
		$criteria->compare('t.name',$this->name,true);
		$criteria->compare('t.description',$this->description,true);
		$criteria->compare('prize.name',$this->prizeId,true);
		$criteria->compare('t.submitStart',$this->submitStart,true);
		$criteria->compare('t.submitEnd',$this->submitEnd,true);
		$criteria->compare('t.voteStart',$this->voteStart,true);
		$criteria->compare('t.voteEnd',$this->voteEnd,true);
		$criteria->compare('t.created',$this->created,true);
		$criteria->compare('t.updated',$this->updated,true);

		$criteria->addCondition('t.deleted=0');

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * @return boolean whether submissions are open for this contest.
	 */
	public function isSubmissionOpen()
	{
		$timeNow = time();
		return $timeNow >= strtotime($this->submitStart) && $timeNow <= strtotime($this->submitEnd);
	}

	/**
	 * Returns whether a specific user has submitted an entry for this contest.
	 * @param integer $userId the user ID
	 * @return boolean
	 */
	public function hasSubmittedEntry($userId)
	{
		$entry = Entry::model()->findByAttributes(array(
			'contestId'=>$this->id,
			'creatorId'=>$userId,
		));

		return $entry !== null;
	}

	/**
	 * @return boolean whether voting is allowed for this contest.
	 */
	public function isVotingAllowed()
	{
		$timeNow = time();
		return $timeNow >= strtotime($this->voteStart) && $timeNow <= strtotime($this->voteEnd);
	}
}